package com.zwd.deep.controller;


import com.zwd.deep.common.RequestResult;
import com.zwd.deep.entity.dto.DoctorsPageDTO;
import com.zwd.deep.entity.dto.UsersDTO;
import com.zwd.deep.entity.dto.UsersLoginDto;
import com.zwd.deep.entity.dto.UsersPageDTO;
import com.zwd.deep.entity.vo.DoctorsVo;
import com.zwd.deep.entity.vo.UsersVo;
import com.zwd.deep.service.IUsersService;
import com.zwd.deep.utils.HttpServletUtil;
import com.zwd.deep.utils.JwtUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户信息表
 *
 * @author zwd
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/users")
@AllArgsConstructor
@Slf4j
@Validated
public class UsersController {

    private final IUsersService usersService;

    /**
     * 登录
     * @param usersLoginDto 用户登录参数
     * @return token
     */
    @PostMapping("/login")
    public RequestResult<String> login(@Validated @RequestBody UsersLoginDto usersLoginDto) {
        String token = usersService.login(usersLoginDto);
        return RequestResult.success(token);
    }

    /**
     * 注册
     * @param usersDTO 注册参数
     * @return UserVo
     */
    @PostMapping("/register")
    public RequestResult<UsersVo> register(@Validated @RequestBody UsersDTO usersDTO) {
        UsersVo usersVo = usersService.register(usersDTO);
        return RequestResult.success(usersVo);
    }

    /**
     * 用户id获取用户信息
     * @return UserVo
     */
    @GetMapping("/getByIdentity")
    public RequestResult<UsersVo> getByIdentity() {
        String userId = JwtUtil.extractId(HttpServletUtil.getToken());
        UsersVo usersVo = usersService.getByUserId(userId);
        return RequestResult.success(usersVo);
    }

    /**
     * 更新用户信息
     * @param usersDTO 用户信息
     * @return Boolean
     */
    @PutMapping("/update")
    public RequestResult<Boolean> update(@RequestBody UsersDTO usersDTO) {
        boolean result = usersService.updateByUserId(usersDTO);
        return RequestResult.bool(result);
    }

//    /**
//     * 上传头像
//     * @param avatar 文件
//     * @return String
//     */
//    @PostMapping("avatar/upload")
//    public RequestResult<String> uploadAvatar(@RequestBody MultipartFile avatar) {
//        return RequestResult.success(userService.uploadAvatar(avatar));
//    }

    /**
     * 分页查询
     * @param usersPageDTO 分页参数
     * @return 分页结果
     */
    @PostMapping("/page")
    public RequestResult<List<UsersVo>> page(@RequestBody UsersPageDTO usersPageDTO) {
        List<UsersVo> usersVos = usersService.getPage(usersPageDTO);
        return RequestResult.success(usersVos);
    }

}
